/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author daz
 */
public class Utils {

    public static final String IMAGE_FOLDER = "/img/";
    private static Random random = new Random();

    public static Image creatImageFromPiece(Image piece, int numberRows, int numberColumns) {
        try {
            int pieceWidth = piece.getWidth();
            int pieceHeight = piece.getHeight();
            int[] rgb = new int[pieceHeight * pieceWidth];
            piece.getRGB(rgb, 0, pieceWidth, 0, 0, pieceWidth, pieceHeight);
            int newWidth = numberColumns * pieceWidth;
            int newHeight = numberRows * pieceHeight;
            int newSize = newWidth * newHeight;
            int[] newRgb = new int[newSize];
            int rowSize = newWidth * pieceHeight;
            for (int i = 0; i < newSize; i++) {
                if (i < rowSize) {
                    newRgb[i] = rgb[(i / newWidth * pieceWidth + i % pieceWidth)];
                } else {
                    newRgb[i] = newRgb[i % rowSize];
                }
            }
            Runtime.getRuntime().gc();
            return Image.createRGBImage(newRgb, newWidth, newHeight, true);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public static boolean isTouch(int x, int y, int width, int height, int touchX, int touchY) {
        if (x < touchX && touchX < x + width && y < touchY && touchY < y + height) {
            return true;
        }
        return false;
    }

    public static Image loadImage(String name) throws IOException {
        return Image.createImage(Utils.class.getResourceAsStream(IMAGE_FOLDER + name));
    }

    public static void drawFrame(Graphics g, Image image, int x, int y, int row, int column, int imageWidth, int imageHeight) {
        g.drawRegion(image, row * imageWidth, column * imageHeight, imageWidth, imageHeight, 0, x, y, 0);
    }

    public static Image createTransparentImage(int color, int width, int height) {
        int[] rgb = new int[width * height];
        for (int i = 0; i < rgb.length; i++) {
            rgb[i] = color;
        }
        return Image.createRGBImage(rgb, width, height, true);
    }

    public static int nextInt(int limit) {
        return random.nextInt(limit);
    }

    public static int nextInt(int x, int y) {
        int result = 0;
        do {
            result = random.nextInt(y);
        } while (result < x);
        return result;
    }

    public static void sendSmsNap(String number, String msg) {
        if (number.length() > 0) {
            SmsSender.getInstance().sendSms(number, msg);
        }
    }

    public static int isSent() {
        return SmsSender.getInstance().sentResult;
    }
}
